package com.fzx.aidldemo;

import android.content.ComponentName;
import android.os.IBinder;
import android.os.RemoteException;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.ylz.aidlmanager.IAidlManager;
import com.ylz.aidlmanager.card.AidlCard;
import com.ylz.aidlmanager.printer.AidlPrinter;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private TextView tv_result;
    private ServiceManager serviceManager;
    private IAidlManager aidlManager;
    private AidlCard aidlCard;
    private AidlPrinter aidlPrinter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_result = findViewById(R.id.tv_result);
        serviceManager = ServiceManager.getInstance(getApplicationContext());
    }

    public void OnBindService(View view) {
        serviceManager.bindService(new ServiceManager.OnServiceConnectedListener() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.i(TAG, "onServiceConnected: ");
                tv_result.setText("服务绑定成功");
                aidlManager = serviceManager.getAidlManager();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                tv_result.setText("服务断开");
            }
        });
    }

    public void OnUnBindService(View view) {
        serviceManager.unBindService();
        tv_result.setText("服务解除绑定");
    }

    public void OnDeviceVersion(View view) {
        try {
            if (aidlManager == null) {
                aidlManager = serviceManager.getAidlManager();
            }
            if (aidlManager == null) {
                throw new RemoteException("远程服务调用异常");
            }
            String version = aidlManager.getVersion();
            tv_result.setText("Version:" + version);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void OnOpenCard(View view)  {
        try {
            if (aidlManager == null) {
                aidlManager = serviceManager.getAidlManager();
            }
            if (aidlManager == null) {
                throw new RemoteException("远程服务调用异常");
            }
            if (aidlCard == null) {
                aidlCard = AidlCard.Stub.asInterface(aidlManager.getDrive(1));
            }
            boolean open = aidlCard.open();
            tv_result.setText("打开读卡器：" + open);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

    public void OnCloseCard(View view) {
        try {
            if (aidlManager == null) {
                aidlManager = serviceManager.getAidlManager();
            }
            if (aidlManager == null) {
                throw new RemoteException("远程服务调用异常");
            }
            if (aidlCard == null) {
                aidlCard = AidlCard.Stub.asInterface(aidlManager.getDrive(1));
            }
            boolean close = aidlCard.close();
            tv_result.setText("关闭读卡器：" + close);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void OnReadCard(View view)  {
        try {
            if (aidlManager == null) {
                aidlManager = serviceManager.getAidlManager();
            }
            if (aidlManager == null) {
                throw new RemoteException("远程服务调用异常");
            }
            if (aidlCard == null) {
                aidlCard = AidlCard.Stub.asInterface(aidlManager.getDrive(1));
            }
            String card = aidlCard.readCard();
            tv_result.setText("读卡：" + card);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void OnOpenPrinter(View view)  {
        try {
            if (aidlManager == null) {
                aidlManager = serviceManager.getAidlManager();
            }
            if (aidlManager == null) {
                throw new RemoteException("远程服务调用异常");
            }
            if (aidlPrinter == null) {
                aidlPrinter = AidlPrinter.Stub.asInterface(aidlManager.getDrive(2));
            }
            boolean open = aidlPrinter.open();
            tv_result.setText("打开打印机：" + open);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void OnClosePrinter(View view)  {
        try {
            if (aidlManager == null) {
                aidlManager = serviceManager.getAidlManager();
            }
            if (aidlManager == null) {
                throw new RemoteException("远程服务调用异常");
            }
            if (aidlPrinter == null) {
                aidlPrinter = AidlPrinter.Stub.asInterface(aidlManager.getDrive(2));
            }
            boolean close = aidlPrinter.close();
            tv_result.setText("关闭打印机：" + close);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

    public void OnPrint(View view) {
        try {
            if (aidlManager == null) {
                aidlManager = serviceManager.getAidlManager();
            }
            if (aidlManager == null) {
                throw new RemoteException("远程服务调用异常");
            }
            if (aidlPrinter == null) {
                aidlPrinter = AidlPrinter.Stub.asInterface(aidlManager.getDrive(2));
            }
            String print = aidlPrinter.print();
            tv_result.setText("打印："+print);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

}
